﻿using Springwater.DataAccessLayer.Model;
using Springwater.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Springwater.DataAccessLayer.Repository
{
    public class BorvizRepository : GenericRepository<Borviz>
    {
        public BorvizRepository(SWModelContainer context)
            : base(context)
        {
        }

        public Borviz GetByKod(string kod)
        {
            return this.dbSet.FirstOrDefault(b => b.Kod.Equals(kod, StringComparison.OrdinalIgnoreCase));
        }

        public List<string[]> Search(out int total, DataTableModel dataTableModel, SearchParamModel searchParam)
        {
            var queryBase = this.dbSet.Join(this.context.Elnevezes, b => b.Id, e => e.BorvizId, (b, e) => new { Borviz = b, Elnevezes = e, Vizgyujto = b.Vizgyujtok.OrderBy(v => v.Rang).FirstOrDefault() })
                                  .Where(x => x.Elnevezes.Elsodleges);
                                  

            #region Filtering

            if (!string.IsNullOrEmpty(searchParam.Kod))
            {
                queryBase = queryBase.Where(x => x.Borviz.Kod.Contains(searchParam.Kod));
            }

            if (!string.IsNullOrEmpty(searchParam.Nev))
            {
                queryBase = queryBase.Where(x => x.Elnevezes.Nev.Contains(searchParam.Nev));
            }

            if (!string.IsNullOrEmpty(searchParam.Tajegyseg))
            {
                queryBase = queryBase.Where(x => x.Borviz.Tajegyseg.Contains(searchParam.Tajegyseg));
            }

            if (!string.IsNullOrEmpty(searchParam.Vizgyujto))
            {
                int id = 0;

                if (Int32.TryParse(searchParam.Vizgyujto, out id))
                {
                    queryBase = queryBase.Where(x => x.Vizgyujto.Id == id);
                }
            }

            #endregion

            var query = queryBase.Select(x => new TableItemModel()
                                    {
                                        Id = x.Borviz.Id,
                                        Kod = x.Borviz.Kod,
                                        Nev = x.Elnevezes.Nev,
                                        Tajegyseg = x.Borviz.Tajegyseg,
                                        Eredet = x.Borviz.Eredet,
                                        Vizgyujto = x.Vizgyujto.Nev
                                    });

            total = query.Count();

            #region Sorting

            var columns = dataTableModel.sColumns.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            switch (columns[Int32.Parse(dataTableModel.iSortCol_0)])
            {
                case "Kod":
                    if (dataTableModel.sSortDir_0.Equals("asc", StringComparison.OrdinalIgnoreCase))
                    {
                        query = query.OrderBy(x => x.Kod);
                    }
                    else
                    {
                        query = query.OrderByDescending(x => x.Kod);
                    }
                    break;
                case "Nev":
                    if (dataTableModel.sSortDir_0.Equals("asc", StringComparison.OrdinalIgnoreCase))
                    {
                        query = query.OrderBy(x => x.Nev);
                    }
                    else
                    {
                        query = query.OrderByDescending(x => x.Nev);
                    }
                    break;
                case "Tajegyseg":
                    if (dataTableModel.sSortDir_0.Equals("asc", StringComparison.OrdinalIgnoreCase))
                    {
                        query = query.OrderBy(x => x.Tajegyseg);
                    }
                    else
                    {
                        query = query.OrderByDescending(x => x.Tajegyseg);
                    }
                    break;
                case "Vizgyujto":
                    if (dataTableModel.sSortDir_0.Equals("asc", StringComparison.OrdinalIgnoreCase))
                    {
                        query = query.OrderBy(x => x.Vizgyujto);
                    }
                    else
                    {
                        query = query.OrderByDescending(x => x.Vizgyujto);
                    }
                    break;
                default:
                        query = query.OrderBy(x => x.Id);
                    break;
            }

            #endregion

            #region Paging

            query = query.Skip(dataTableModel.iDisplayStart)
                         .Take(dataTableModel.iDisplayLength);

            #endregion

            return query.ToArray().Select(x => new string[]
            {
                x.Id.ToString(),
                x.Kod,
                x.Nev,
                x.Tajegyseg,
                x.Vizgyujto,
                x.Eredet, 
                null,
                null,
                null
            }).ToList();
        }
    }
}
